﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace gammmaertld
{
    class InputManager : GameComponent
    {
        static KeyboardState previousKeyboardState;
        static KeyboardState currentKeyboardState;

        static MouseState previousMouseState;
        static MouseState currentMouseState;

        public InputManager(Game game)
            : base(game)
        {
            currentKeyboardState = Keyboard.GetState();
            currentMouseState    = Mouse.GetState();

            previousKeyboardState = currentKeyboardState;
            previousMouseState  = currentMouseState;
        }

        public override void Update(GameTime gameTime)
        {
            Update();
            base.Update(gameTime);
        }

        static public void Update()
        {
            previousKeyboardState = currentKeyboardState;
            previousMouseState    = currentMouseState;

            currentKeyboardState = Keyboard.GetState();
            currentMouseState    = Mouse.GetState();
        }


        //Keyboard State Stuff
        static public bool IsKeyTriggered(Keys key)
        {
            return previousKeyboardState.IsKeyUp(key) && currentKeyboardState.IsKeyDown(key);
        }

        static public bool IsKeyPressed(Keys key)
        {
            return previousKeyboardState.IsKeyDown(key) && currentKeyboardState.IsKeyDown(key);
        }

        static public bool IsKeyUp(Keys key)
        {
            return previousKeyboardState.IsKeyUp(key) && currentKeyboardState.IsKeyUp(key);
        }

        static public Keys[] GetPressedKeys
        {
            get { return currentKeyboardState.GetPressedKeys(); }
        }

        //Mouse State Stuff
        static public bool IsLButtonTriggered
        {
            get { return previousMouseState.LeftButton.Equals(ButtonState.Released) && currentMouseState.LeftButton.Equals(ButtonState.Pressed); }
        }
        static public bool IsLButtonPressed
        {
            get { return currentMouseState.LeftButton.Equals(ButtonState.Pressed); }
        }
        static public bool IsLButtonReleased
        {
            get { return previousMouseState.LeftButton.Equals(ButtonState.Released) && currentMouseState.LeftButton.Equals(ButtonState.Released); }
        }

        static public bool IsRButtonTriggered
        {
            get { return previousMouseState.RightButton.Equals(ButtonState.Released) && currentMouseState.RightButton.Equals(ButtonState.Pressed); }
        }
        static public bool IsRButtonPressed
        {
            get { return previousMouseState.RightButton.Equals(ButtonState.Pressed) && currentMouseState.RightButton.Equals(ButtonState.Pressed); }
        }
        static public bool IsRButtonReleased
        {
            get { return previousMouseState.RightButton.Equals(ButtonState.Released) && currentMouseState.RightButton.Equals(ButtonState.Released); }
        }

        static public bool IsMButtonTriggered
        {
            get { return previousMouseState.MiddleButton.Equals(ButtonState.Released) && currentMouseState.MiddleButton.Equals(ButtonState.Pressed); }
        }
        static public bool IsMButtonPressed
        {
            get { return previousMouseState.MiddleButton.Equals(ButtonState.Pressed) && currentMouseState.MiddleButton.Equals(ButtonState.Pressed); }
        }
        static public bool IsMButtonReleased
        {
            get { return previousMouseState.MiddleButton.Equals(ButtonState.Released) && currentMouseState.MiddleButton.Equals(ButtonState.Released); }
        }

        static public Vector2 MouseLocation
        {
            get { return new Vector2(currentMouseState.X, currentMouseState.Y); }
        }
        static public int MouseXLocation
        {
            get { return currentMouseState.X; }
        }
        static public int MouseYLocation
        {
            get { return currentMouseState.Y; }
        }
    }
}
